<template>
  <div>
    <div class="row-header">
      <el-form :inline="true" label-width="150px">
        <el-form-item label="任务类型" label-width="100px">
          <el-select v-model="dictTaskType" placeholder="任务类型" style="width: 300px;" clearable>
            <el-option v-for="task in dictTaskTypeSelect" :key="task.value" :label="task.label"
                       :value="task.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态" label-width="100px">
          <el-select v-model="dictStatus" placeholder="处理状态" style="width: 300px;" clearable>
            <el-option v-for="state in dictStatusSelect" :key="state.value" :label="state.label"
                       :value="state.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理时间" label-width="100px">
          <el-date-picker class="select" v-model="taskTime" type="datetime" align="right" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
      </el-form>
      <div class="row-form-btn">
        <el-button type="shenda" size="small" icon="el-icon-search" @click="currentPage = 1 ; initData()">
          查询
        </el-button>
      </div>
    </div>
    <div class="split-line"></div>
    <div class="row-main">
      <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData"
                tooltip-effect="dark" class="row-main-table" @selection-change="handleSelectionChange"
                v-loading.fullscreen="tableLoading" element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="taskName" label="任务名称" align="center"></el-table-column>
        <el-table-column prop="cardNum" label="卡总量" align="center"></el-table-column>
        <el-table-column prop="finishNumber" label="完成进度" align="center"></el-table-column>
        <el-table-column prop="failNum" label="失败数量" align="center"></el-table-column>
        <el-table-column prop="dictStatusName" label="任务状态" align="center"></el-table-column>
        <el-table-column prop="insertTime" label="开始时间" align="center"></el-table-column>
        <el-table-column prop="taskEndTime" label="完成时间" align="center"></el-table-column>
        <el-table-column prop="dictTaskType" label="任务类型" align="center"></el-table-column>
      </el-table>
      <div class="block"><span class="demonstration"></span>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="total" class="row-main-pager"
                       :current-page.sync="currentPage" :page-size="pageSize" layout="total,sizes,prev, pager, next, jumper"
                       :page-sizes="[10,20,50,100]" background></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {taskInstance} from '@/api/getData'

export default {
  name: 'MissionSchedule',
  data() {
    return {
      maxPage: 1,
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      tableLoading: false,
      value: false,
      dictTaskType: '',
      dictTaskTypeSelect: [
        {
          value: 'FLOW_POLLING',
          label: '流量任务'
        },
        {
          value: 'STATUS_POLLING',
          label: '状态任务'
        },
        {
          value: 'STATUS_LIMIT',
          label: '状态任务'
        },
        {
          value: 'JIUTIAN_PACKAGE',
          label: '九天套餐自动导入'
        },
        {
          value: 'JIUTIAN_IOT_PACKAGE',
          label: '九天新平台套餐自动导入'
        },
        {
          value: 'KAHAO_PACKAGE',
          label: '业务号码套餐自动导入'
        }
      ],
      taskTime: '',
      dictStatus: '',
      dictStatusSelect: [
        {
          value: 'TASK_COMPLETED',
          label: '完成'
        },
        {
          value: 'TASK_PENDING',
          label: '进行中'
        },
        {
          value: 'TASK_FAIL',
          label: '任务失败'
        }
      ],
      taskName: '',
      cardNum: '',
      finishNumber: '',
      failNum: '',
      insertTime: '',
      taskEndTime: '',
      dictStatusName: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      this.tableLoading = false;
      const returnData = await taskInstance({
          taskTime: this.taskTime,
          dictTaskType: this.dictTaskType,
          dictStatus: this.dictStatus,
          page: {
            current: this.currentPage + '',
            size: this.pageSize + '',
            ascs: this.ascs,
            descs: this.descs
          }
        },
        {
          obj: this,
          key: 'tableLoading'
        });
      this.tableData = []
      this.total = 0;
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
        returnData.data.records.map(item => {
          const obj = {}
          obj.taskName = item.taskName // 任务名称
          obj.cardNum = item.cardNum // 卡总量
          obj.finishNum = item.finishNum // 完成进度
          obj.failNum = item.failNum // 失败数量
          obj.dictStatus = item.dictStatus // 任务状态
          obj.dictStatusName = item.dictStatusName // 任务状态名字
          obj.insertTime = item.insertTime // 开始时间
          obj.taskEndTime = item.taskEndTime // 完成时间
          obj.dictTaskType = item.dictTaskType // 任务类型
          obj.finishNumber = (item.finishNum / item.cardNum) * 100 + '%'
          if (item.finishNum === 0 || item.cardNum === 0) {
            obj.finishNumber = 0
          }
          if (item.dictTaskType === 'MIDDLE_PENDING') {
            obj.dictTaskType = '处理中'
          }
          if (item.dictTaskType === 'MIDDLE_FAIL') {
            obj.dictTaskType = '处理失败'
          }
          if (item.dictTaskType === 'MIDDLE_COMPLETED') {
            obj.dictTaskType = '处理完成'
          }
          if (item.dictTaskType === 'MIDDLE_NEW') {
            obj.dictTaskType = '新建'
          }
          this.tableData.push(obj)
        })
      }
      this.total = returnData.data.total;
      this.tableLoading = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.initData()
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    }
  }
}
</script>

<style scoped lang="scss">

.examine /deep/ .el-dialog__body {
  text-align: center;
  font-size: 14px;
  font-family: 微软雅黑;
  line-height: 40px;
  color: #606266;
  font-weight: 700;
}

.el-popper[x-placement^=bottom] {
  margin-right: -25px;
  box-shadow: 0px 0px 10px #888888;
  margin-left: 6px;

  .operation {
    border: 0;
    outline: none;
    display: block;
    text-align: center;
    margin: 0;
  }
}

.el-popper[x-placement^=top] {
  box-shadow: 0px 0px 10px #888888;
  margin-left: 6px;
  margin-right: -25px;

  button {
    display: block;
  }

  .operation {
    border: 0;
    outline: none;
    display: block;
    text-align: center;
    margin: 0;
  }
}
</style>
